package name.fowler.refactoring.simplestore.dao;

import name.fowler.refactoring.simplestore.model.Customer;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class CustomerDao {

    private List<Customer> customers;

    public CustomerDao () {
        customers = new LinkedList<Customer>();
    }

    public Customer findByName(String name) throws CustomerNotFoundException {
        for (Customer customer : customers) {
            if (Objects.equals(name, customer.getName())) {
                return customer;
            }
        }
        throw new CustomerNotFoundException(name);
    }

    public Customer save(Customer customer) {
        customers.add(customer);
        return customer;
    }

    public List<Customer> save(Collection<Customer> customers) {
        this.customers.addAll(customers);
        return this.customers;
    }
}
